package com.zhu.service.util.handler.utils;

import com.zhu.model.pojo.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单工具类
 */
public class MenuHelper {
    //构建树形结构
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        List<SysMenu> trees = new ArrayList<>();
        //遍历结合
        for (SysMenu sysMenu : sysMenuList) {
             //找到递归的入口
            if(sysMenu.getParentId().longValue()==0){
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> treeNodes) {
        sysMenu.setChildren(new ArrayList<>());
        //遍历递归进行查找
        for (SysMenu node : treeNodes) {
            if(node.getParentId().longValue()==Long.valueOf(sysMenu.getId())){
                sysMenu.getChildren().add(findChildren(node,treeNodes));
            }
        }
        return sysMenu;
    }
}
